#include <iostream>
using namespace std;
class Solution {
public:
    int reverse(int x) {

        int ceil = 2147483648;

        int mark = x > 0 ? 1:-1;
        int v = x>0?x:-x;
        long int r = 0;
        if (x > -10 && x < 10) {
            return x;
        }

        if (x == -ceil) {
            return 0;
        }

        int digit;
        while(v) {
            digit = v%10;
            if (digit || r != 0) {
                r = r*10+digit;
            }
            v = v/10;
        }

        if (r == ceil && !mark) {
            return -ceil;
        }

        if (r > ceil-1) {
            return 0;
        }
        return r * mark;

    }
};


int main() {
    Solution s;
    cout << s.reverse(123) << endl;
    cout << s.reverse(-2147483648) << endl;
}

